// Qt on Metro Stub
// Copyright (c) Andrew Knight
// This software is licensed under the MIT: http://www.opensource.org/licenses/MIT

// Based on code from http://code.msdn.microsoft.com/windowsapps/Direct2D-Basic-Animation-d0a5a4d1, original license:
//// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
//// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
//// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//// PARTICULAR PURPOSE.
////
//// Copyright (c) Microsoft Corporation. All rights reserved

#include "DirectXViewProvider.h"
#include "View.h"

using namespace Windows::ApplicationModel::Core;
using namespace Windows::UI::Core;
using namespace Windows::ApplicationModel::Infrastructure;
using namespace Windows::ApplicationModel::Activation;

DirectXViewProvider::DirectXViewProvider()
{
    m_activationEntryPoint = ActivationEntryPoint::Unknown;
}

void DirectXViewProvider::Initialize(
    _In_ Windows::UI::Core::CoreWindow^ window,
    _In_ Windows::ApplicationModel::Core::CoreApplicationView^ applicationView
    )
{
    m_window = window;
    m_applicationView = applicationView;
}

// this method is called after Initialize
void DirectXViewProvider::Load(Platform::String^ entryPoint)
{
    if(entryPoint == "DirectXApplication.App")
    {
        m_activationEntryPoint = ActivationEntryPoint::DirectXApplication;
    }
}

// this method is called after Load
void DirectXViewProvider::Run()
{
    if (m_activationEntryPoint == ActivationEntryPoint::DirectXApplication)
    {
        auto view = ref new View();
        view->Initialize(m_window, m_applicationView);
        view->Run();

        // Must delete the view explicitly in order to break a circular dependency
        // between View and CoreWindow. View holds on to a CoreWindow reference most
        // typically for window activation, while CoreWindow refers back to View when
        // event handlers are hooked up. Without breaking this circular dependency,
        // neither View nor CoreWindow object gets to clean up. It's also important
        // to note that a 'delete' call on a ref class instance simply means calling
        // into a class destructor in order to explicitly break a cycle. It doesn't
        // actually deallocate any memory.
        delete view;
    }
    else
    {
        DX::ThrowIfFailed(E_UNEXPECTED);
    }
}

void DirectXViewProvider::Uninitialize()
{
}

IViewProvider^ DirectXViewProviderFactory::CreateViewProvider()
{
    return ref new DirectXViewProvider();
}
